package myclient;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/**
 * @author JIAO
 */
public class GetIP implements ActionListener{

	private JTextField textIP;
	String ip = null;
	Vector<String> v;
	JFrame fm;
	
	/**
	 * 	输入框，用来获得服务器的IP地址
	 */
	public GetIP() {
		fm = new JFrame();
		fm.setBounds(200, 200, 250, 200);
		//用来装历史IP的数组
		v = new Vector<String>();
		try {
			//从文件中读取IP地址
			InputStream input = this.getClass().getResourceAsStream("/ip/ip.txt");
			BufferedReader reader = new BufferedReader(new InputStreamReader(input));
			String line = null;
			while((line=reader.readLine())!=null){
				v.add(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JPanel pnText = new JPanel();
		textIP = new JTextField();
		textIP.setPreferredSize(new Dimension(100,25));
		textIP.setText(v.elementAt(0));
		JLabel lbIP = new JLabel("服务器IP:");
		JLabel lbSelect = new JLabel("select");
		pnText.add(lbIP);
		pnText.add(textIP);
		pnText.add(lbSelect);
	
		final JPanel pnList = new JPanel();
		final JList list = new JList(v);
		JScrollPane scroPane = new JScrollPane(list);
		pnList.add(scroPane);
		pnList.setVisible(false);
		lbSelect.addMouseListener(new MouseListener(){
			public void mouseClicked(MouseEvent e) {
				pnList.setVisible(!pnList.isVisible());
			}
			public void mouseEntered(MouseEvent e) {
			}
			public void mouseExited(MouseEvent e) {
			}
			public void mousePressed(MouseEvent e) {
			}
			public void mouseReleased(MouseEvent e) {
			}});
		list.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				textIP.setText(list.getSelectedValue().toString());
				pnList.setVisible(false);
			}
		});
		JPanel pnBt = new JPanel();
		JButton bt = new JButton("确定");
		pnBt.add(bt);
		bt.addActionListener(this);
		
		fm.getContentPane().setLayout(new BorderLayout());
		fm.getContentPane().add(pnText,BorderLayout.NORTH);
		fm.getContentPane().add(pnList,BorderLayout.CENTER);
		fm.getContentPane().add(pnBt,BorderLayout.SOUTH);
//		System.out.println(scroPane.getSize().toString());
		fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fm.setVisible(true);
		
		
	}
	//Button监听器
	public void actionPerformed(ActionEvent e) {
		String s = textIP.getText().trim();
		StringTokenizer tokens = new StringTokenizer(s,".");
		if(!s.equals("localhost")&&tokens.countTokens()!=4){
			JOptionPane.showMessageDialog(null, "格式不正确，请重新填写");
			ip = null;
			return;
		}
		ip = s;
		if(v.contains(ip))
			v.remove(ip);
		v.insertElementAt(ip, 0);
//		try {
//			URL r = this.getClass().getResource("/ip/ip.txt");
//			String str = r.getFile();
//			JOptionPane.showMessageDialog(null, r.toString());
//			File f = new File(r.toURI());
//			JOptionPane.showMessageDialog(null, f.getAbsolutePath());
//			PrintWriter w = new PrintWriter(new FileWriter(f));
//			Iterator<String> it =  v.iterator();
//			while(it.hasNext()){
//				w.println(it.next());
//			}
//			w.close();
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		} catch (URISyntaxException ee) {
//			// TODO Auto-generated catch block
//			ee.printStackTrace();
//		}
		fm.dispose();
		new LoginFrame(ip);
	
		
	}
	

}
